import { NextRequest, NextResponse } from 'next/server'
import { prisma } from '@/lib/db'
import { getUserFromRequest } from '@/lib/auth'

export async function DELETE(request: NextRequest) {
  try {
    const user = getUserFromRequest(request)
    
    if (!user) {
      return NextResponse.json(
        { error: '未授权访问' },
        { status: 401 }
      )
    }

    const { videoIds } = await request.json()

    if (!Array.isArray(videoIds) || videoIds.length === 0) {
      return NextResponse.json(
        { error: '请提供要删除的视频ID列表' },
        { status: 400 }
      )
    }

    // 验证所有ID都是数字
    const validIds = videoIds.filter(id => typeof id === 'number' && !isNaN(id))
    
    if (validIds.length === 0) {
      return NextResponse.json(
        { error: '无效的视频ID列表' },
        { status: 400 }
      )
    }

    // 批量删除视频
    const result = await prisma.tikTokVideo.deleteMany({
      where: {
        id: {
          in: validIds
        }
      }
    })

    return NextResponse.json({
      message: `成功删除 ${result.count} 个视频`,
      deletedCount: result.count
    })

  } catch (error) {
    console.error('Batch delete videos error:', error)
    return NextResponse.json(
      { error: '批量删除失败' },
      { status: 500 }
    )
  }
}
